<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>KosarajuSharirSCC.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="KosarajuSharirSCC code in Java">
<meta NAME="TITLE" CONTENT="KosarajuSharirSCC code in Java">
<meta NAME="KEYWORDS" CONTENT="KosarajuSharirSCC,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>KosarajuSharirSCC.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "KosarajuSharirSCC.java">KosarajuSharirSCC.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac KosarajuSharirSCC.java</span>
<span class="comment"> *  Execution:    java KosarajuSharirSCC filename.txt</span>
<span class="comment"> *  Dependencies: Digraph.java TransitiveClosure.java StdOut.java In.java</span>
<span class="comment"> *  Data files:   </span><span class="url">http://algs4.cs.princeton.edu/42digraph/tinyDG.txt</span>
<span class="comment"> *</span>
<span class="comment"> *  Compute the strongly-connected components of a digraph using the</span>
<span class="comment"> *  Kosaraju-Sharir algorithm.</span>
<span class="comment"> *</span>
<span class="comment"> *  Runs in O(E + V) time.</span>
<span class="comment"> *</span>
<span class="comment"> *  % java KosarajuSCC tinyDG.txt</span>
<span class="comment"> *  5 components</span>
<span class="comment"> *  1 </span>
<span class="comment"> *  0 2 3 4 5 </span>
<span class="comment"> *  9 10 11 12 </span>
<span class="comment"> *  6 8 </span>
<span class="comment"> *  7</span>
<span class="comment"> *</span>
<span class="comment"> *  % java KosarajuSharirSCC mediumDG.txt </span>
<span class="comment"> *  10 components</span>
<span class="comment"> *  21 </span>
<span class="comment"> *  2 5 6 8 9 11 12 13 15 16 18 19 22 23 25 26 28 29 30 31 32 33 34 35 37 38 39 40 42 43 44 46 47 48 49 </span>
<span class="comment"> *  14 </span>
<span class="comment"> *  3 4 17 20 24 27 36 </span>
<span class="comment"> *  41 </span>
<span class="comment"> *  7 </span>
<span class="comment"> *  45 </span>
<span class="comment"> *  1 </span>
<span class="comment"> *  0 </span>
<span class="comment"> *  10 </span>
<span class="comment"> *</span>
<span class="comment"> *  % java -Xss50m KosarajuSharirSCC mediumDG.txt </span>
<span class="comment"> *  25 components</span>
<span class="comment"> *  7 11 32 36 61 84 95 116 121 128 230   ...</span>
<span class="comment"> *  28 73 80 104 115 143 149 164 184 185  ...</span>
<span class="comment"> *  38 40 200 201 207 218 286 387 418 422 ...</span>
<span class="comment"> *  12 14 56 78 87 103 216 269 271 272    ...</span>
<span class="comment"> *  42 48 112 135 160 217 243 246 273 346 ...</span>
<span class="comment"> *  46 76 96 97 224 237 297 303 308 309   ...</span>
<span class="comment"> *  9 15 21 22 27 90 167 214 220 225 227  ...</span>
<span class="comment"> *  74 99 133 146 161 166 202 205 245 262 ...</span>
<span class="comment"> *  43 83 94 120 125 183 195 206 244 254  ...</span>
<span class="comment"> *  1 13 54 91 92 93 106 140 156 194 208  ...</span>
<span class="comment"> *  10 39 67 69 131 144 145 154 168 258   ...</span>
<span class="comment"> *  6 52 66 113 118 122 139 147 212 213   ...</span>
<span class="comment"> *  8 127 150 182 203 204 249 367 400 432 ...</span>
<span class="comment"> *  63 65 101 107 108 136 169 170 171 173 ...</span>
<span class="comment"> *  55 71 102 155 159 198 228 252 325 419 ...</span>
<span class="comment"> *  4 25 34 58 70 152 172 196 199 210 226 ...</span>
<span class="comment"> *  2 44 50 88 109 138 141 178 197 211    ...</span>
<span class="comment"> *  57 89 129 162 174 179 188 209 238 276 ...</span>
<span class="comment"> *  33 41 49 119 126 132 148 181 215 221  ...</span>
<span class="comment"> *  3 18 23 26 35 64 105 124 157 186 251  ...</span>
<span class="comment"> *  5 16 17 20 31 47 81 98 158 180 187    ...</span>
<span class="comment"> *  24 29 51 59 75 82 100 114 117 134 151 ...</span>
<span class="comment"> *  30 45 53 60 72 85 111 130 137 142 163 ...</span>
<span class="comment"> *  19 37 62 77 79 110 153 352 353 361    ...</span>
<span class="comment"> *  0 68 86 123 165 176 193 239 289 336   ...</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">KosarajuSharirSCC</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class represents a data type for </span>
<span class="comment"> *  determining the strong components in a digraph.</span>
<span class="comment"> *  The </span><span class="keyword">&lt;em&gt;</span><span class="comment">id</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> operation determines in which strong component</span>
<span class="comment"> *  a given vertex lies; the </span><span class="keyword">&lt;em&gt;</span><span class="comment">areStronglyConnected</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> operation</span>
<span class="comment"> *  determines whether two vertices are in the same strong component;</span>
<span class="comment"> *  and the </span><span class="keyword">&lt;em&gt;</span><span class="comment">count</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> operation determines the number of strong</span>
<span class="comment"> *  components.</span>

<span class="comment"> *  The </span><span class="keyword">&lt;em&gt;</span><span class="comment">component identifier</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> of a component is one of the</span>
<span class="comment"> *  vertices in the strong component: two vertices have the same component</span>
<span class="comment"> *  identifier if and only if they are in the same strong component.</span>

<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  This implementation uses the Kosaraju-Sharir algorithm.</span>
<span class="comment"> *  The constructor takes time proportional to </span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> + </span><span class="keyword">&lt;em&gt;</span><span class="comment">E</span><span class="keyword">&lt;/em&gt;</span>
<span class="comment"> *  (in the worst case),</span>
<span class="comment"> *  where </span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is the number of vertices and </span><span class="keyword">&lt;em&gt;</span><span class="comment">E</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is the number of edges.</span>
<span class="comment"> *  Afterwards, the </span><span class="keyword">&lt;em&gt;</span><span class="comment">id</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">count</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, and </span><span class="keyword">&lt;em&gt;</span><span class="comment">areStronglyConnected</span><span class="keyword">&lt;/em&gt;</span>
<span class="comment"> *  operations take constant time.</span>
<span class="comment"> *  For alternate implementations of the same API, see</span>
<span class="comment"> *  {</span><span class="type">@link</span><span class="comment"> TarjanSCC} and {</span><span class="type">@link</span><span class="comment"> GabowSCC}.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation,</span>
<span class="comment"> *  see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/42digraph"</span><span class="keyword">&gt;</span><span class="comment">Section 4.2</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">KosarajuSharirSCC</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="symbol">[]</span><span class="normal"> marked</span><span class="symbol">;</span><span class="normal">     </span><span class="comment">// marked[v] = has vertex v been visited?</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> id</span><span class="symbol">;</span><span class="normal">             </span><span class="comment">// id[v] = id of strong component containing v</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> count</span><span class="symbol">;</span><span class="normal">            </span><span class="comment">// number of strongly-connected components</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Computes the strong components of the digraph </span><span class="keyword">&lt;tt&gt;</span><span class="comment">G</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> G the digraph</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">KosarajuSharirSCC</span><span class="symbol">(</span><span class="usertype">Digraph</span><span class="normal"> G</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">        </span><span class="comment">// compute reverse postorder of reverse graph</span>
<span class="normal">        </span><span class="usertype">DepthFirstOrder</span><span class="normal"> dfs </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">DepthFirstOrder</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">.</span><span class="function">reverse</span><span class="symbol">());</span>

<span class="normal">        </span><span class="comment">// run DFS on G, using reverse postorder to guide calculation</span>
<span class="normal">        marked </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">boolean</span><span class="symbol">[</span><span class="normal">G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">()];</span>
<span class="normal">        id </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">()];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">:</span><span class="normal"> dfs</span><span class="symbol">.</span><span class="function">reversePost</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">marked</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">])</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="function">dfs</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">,</span><span class="normal"> v</span><span class="symbol">);</span>
<span class="normal">                count</span><span class="symbol">++;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// check that id[] gives strong components</span>
<span class="normal">        </span><span class="keyword">assert</span><span class="normal"> </span><span class="function">check</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// DFS on graph G</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">dfs</span><span class="symbol">(</span><span class="usertype">Digraph</span><span class="normal"> G</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> v</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span>
<span class="normal">        marked</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">        id</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> count</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> w </span><span class="symbol">:</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">adj</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">marked</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">])</span><span class="normal"> </span><span class="function">dfs</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">,</span><span class="normal"> w</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the number of strong components.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the number of strong components</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">count</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> count</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Are vertices </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">w</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> in the same strong component?</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> v one vertex</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> w the other vertex</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if vertices </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">w</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> are in the same</span>
<span class="comment">     *     strong component, and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">stronglyConnected</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> w</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> id</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> id</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">];</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the component id of the strong component containing vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> v the vertex</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the component id of the strong component containing vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">id</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> id</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">];</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// does the id[] array contain the strongly connected components?</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">check</span><span class="symbol">(</span><span class="usertype">Digraph</span><span class="normal"> G</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">TransitiveClosure</span><span class="normal"> tc </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">TransitiveClosure</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> w </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> w</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">stronglyConnected</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">,</span><span class="normal"> w</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">tc</span><span class="symbol">.</span><span class="function">reachable</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">,</span><span class="normal"> w</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> tc</span><span class="symbol">.</span><span class="function">reachable</span><span class="symbol">(</span><span class="normal">w</span><span class="symbol">,</span><span class="normal"> v</span><span class="symbol">)))</span>
<span class="normal">                    </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Unit tests the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">KosarajuSharirSCC</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> data type.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">In</span><span class="normal"> in </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">In</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="usertype">Digraph</span><span class="normal"> G </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Digraph</span><span class="symbol">(</span><span class="normal">in</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">KosarajuSharirSCC</span><span class="normal"> scc </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">KosarajuSharirSCC</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// number of connected components</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> M </span><span class="symbol">=</span><span class="normal"> scc</span><span class="symbol">.</span><span class="function">count</span><span class="symbol">();</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">M </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" components"</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// compute list of vertices in each strong component</span>
<span class="normal">        Queue</span><span class="symbol">&lt;</span><span class="normal">Integer</span><span class="symbol">&gt;[]</span><span class="normal"> components </span><span class="symbol">=</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">Queue</span><span class="symbol">&lt;</span><span class="normal">Integer</span><span class="symbol">&gt;[])</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Queue</span><span class="symbol">[</span><span class="normal">M</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> M</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            components</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Queue</span><span class="symbol">&lt;</span><span class="normal">Integer</span><span class="symbol">&gt;();</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            components</span><span class="symbol">[</span><span class="normal">scc</span><span class="symbol">.</span><span class="function">id</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">)].</span><span class="function">enqueue</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// print results</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> M</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">:</span><span class="normal"> components</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">])</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">print</span><span class="symbol">(</span><span class="normal">v </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" "</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="cbracket">}</span>

<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
